package demo.Socket.Grass_num_game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.Socket;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/22-08-22-12:00
 * @Description：demo.Socket.Grass_num_game
 */
public class GrassTlicent_GUI {
    public static void main(String[] args) {
        JFrame frame = new JFrame("猜数字游戏");
        frame.setBounds(300,400,400,300);
        GridLayout grid = new GridLayout(3,3);
        frame.setLayout(new GridLayout(3,3));
        Socket socket = null;
        try {
            socket = new Socket(PropertiesUtils.properties.getProperty("address"),Integer.parseInt(PropertiesUtils.properties.getProperty("port")) );
            //socket = new Socket("127.0.0.1",6677);
            BufferedReader reader1 = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            JLabel label = new JLabel(reader1.readLine());

            JButton button = new JButton("开始游戏");
            frame.add(label);
            frame.add(new Label());
            frame.add(button);
            Socket finalSocket = socket;
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    JFrame frame2 = new JFrame("猜数字游戏");
                    frame2.setBounds(300,400,400,300);
                    frame2.setLayout(new GridLayout(3,1));
                    JLabel l = new JLabel("请输入您的猜测：");
                    JTextField field = new JTextField();
                    frame2.add(l);
                    frame2.add(field);
                    JButton button1 = new JButton("确认");
                    button1.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String s = field.getText();
                            System.out.println(s);
                            try {
                                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(finalSocket.getOutputStream()));
                                writer.write(s + "\r\n");
                                writer.flush();
                                s = reader1.readLine();
                                JOptionPane.showMessageDialog(frame,s,s,JOptionPane.INFORMATION_MESSAGE);
                                System.out.println(s);
                                if(s.startsWith("恭喜") || s.startsWith("很遗憾")){
                                    frame2.setVisible(false);
                                }
                            } catch (IOException ioException) {
                                ioException.printStackTrace();
                            }
                        }
                    });
                    frame2.add(button1);
                    frame2.setVisible(true);
                }
            });
            frame.setVisible(true);
            //System.out.println(reader1.readLine());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
